package com.ruoyi.bysj.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.bysj.domain.*;
import com.ruoyi.bysj.mapper.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bysj.service.ISysOrderService;

/**
 * 订单Service业务层处理
 * 
 * @author laow
 * @date 2021-10-17
 */
@Service
public class SysOrderServiceImpl implements ISysOrderService 
{
    @Autowired
    private SysOrderMapper sysOrderMapper;

    @Autowired
    private SysGoodsMapper sysGoodsMapper;
    @Autowired
    private SysOrderGoodsMapper sysOrderGoodsMapper;

    @Autowired
    private SysGroupsUserMapper sysGroupsUserMapper;

    @Autowired
    private SysPayMapper sysPayMapper;

    @Autowired
    private SysEvaluationMapper sysEvaluationMapper;

    /**
     * 查询订单
     * 
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public SysOrder selectSysOrderByOrderId(Long orderId)
    {
        SysPay sysPay = sysPayMapper.selectSysPayByOrderId(orderId);
        SysOrder sysOrder = sysOrderMapper.selectSysOrderByOrderId(orderId);
        if (sysPay !=null){
            sysOrder.setGoodsCost(sysPay.getGoodsCost());
            sysOrder.setArtificialCost(sysPay.getArtificialCost());
            sysOrder.setOrderGoods(sysPay.getOrderGoods());
        }
        SysEvaluation sysEvaluation = sysEvaluationMapper.selectSysEvaluationByOrderId(orderId);
        if (sysEvaluation !=null){
            sysOrder.setEvaluationScore(sysEvaluation.getEvaluationScore());
            sysOrder.setEvaluationContent(sysEvaluation.getEvaluationContent());
        }
        return sysOrder;
    }

    /**
     * 查询订单列表
     * 
     * @param sysOrder 订单
     * @return 订单
     */
    @Override
    public List<SysOrder> selectSysOrderList(SysOrder sysOrder)
    {
        sysOrder.setGroupId(sysGroupsUserMapper.selectGroupsUserByUserName(sysOrder.getUserName()));
        List<SysOrder> orderList = sysOrderMapper.selectSysOrderList(sysOrder);
        for (SysOrder order : orderList) {
            SysPay sysPay = sysPayMapper.selectSysPayByOrderId(order.getOrderId());
            if (sysPay !=null){
                order.setGoodsCost(sysPay.getGoodsCost());
                order.setArtificialCost(sysPay.getArtificialCost());
                order.setOrderGoods(sysPay.getOrderGoods());
            }
            SysEvaluation sysEvaluation = sysEvaluationMapper.selectSysEvaluationByOrderId(order.getOrderId());
            if (sysEvaluation !=null){
                order.setEvaluationScore(sysEvaluation.getEvaluationScore());
                order.setEvaluationContent(sysEvaluation.getEvaluationContent());
            }
        }
        return orderList;
    }

    /**
     * 新增订单
     * 
     * @param sysOrder 订单
     * @return 结果
     */
    @Override
    public int insertSysOrder(SysOrder sysOrder)
    {
        sysOrderMapper.insertSysOrder(sysOrder);
        int id = Math.toIntExact(sysOrder.getOrderId());
        int a = 3;
        if (id%3 != 0){
            a = id%3;
        }
        sysOrder.setGroupId((long) a);
        return sysOrderMapper.updateSysOrder(sysOrder);
    }

    /**
     * 修改订单
     * 
     * @param sysOrder 订单
     * @return 结果
     */
    @Override
    public int updateSysOrder(SysOrder sysOrder)
    {
        Long ac = sysOrder.getOrderTime();
        int b = 0;
        int cost = 0;
        List<Long> goodsIds = sysOrder.getGoodsIds();
        for (Long goodsId : goodsIds) {
            BigDecimal a = sysGoodsMapper.selectSysGoodsByGoodsId(goodsId).getGoodsPrice();
            cost = cost + a.intValue();
        }
        b = (int) (ac*20 + cost);
        insertOrdergoods(sysOrder);
        insertPay(sysOrder);
        sysOrder.setOrderCost(String.valueOf(b));
        sysOrder.setOrderEndtime(DateUtils.getNowDate());
        sysOrder.setOrderState("3");
        return sysOrderMapper.updateSysOrder(sysOrder);
    }

    @Override
    public int updateSysPay(SysPay sysPay)
    {
        SysOrder sysOrder = sysOrderMapper.selectSysOrderByOrderId(sysPay.getOrderId());
        sysOrder.setOrderState("4");
        sysOrderMapper.updateSysOrder(sysOrder);
        String name = sysPay.getPayName();
        sysPay = sysPayMapper.selectSysPayByOrderId(sysPay.getOrderId());
        sysPay.setPayTime(DateUtils.getNowDate());
        sysPay.setPayName(name);
        return sysPayMapper.updateSysPay(sysPay);
    }
    /**
     * 批量删除订单
     * 
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteSysOrderByOrderIds(Long[] orderIds)
    {
        return sysOrderMapper.deleteSysOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     * 
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteSysOrderByOrderId(Long orderId)
    {
        return sysOrderMapper.deleteSysOrderByOrderId(orderId);
    }

    public void insertOrdergoods(SysOrder sysOrder)
    {
        List<Long> goods = sysOrder.getGoodsIds();
        if (StringUtils.isNotNull(goods))
        {
            List<SysOrderGoods> list = new ArrayList<SysOrderGoods>();
            for (Long goodsId : goods)
            {
                SysOrderGoods og = new SysOrderGoods();
                og.setOrderId(sysOrder.getOrderId());
                og.setGoodsId(goodsId);
                list.add(og);
            }
            if (list.size() > 0)
            {
                sysOrderGoodsMapper.batchOrderGoods(list);
            }
        }
    }

    public void insertPay(SysOrder sysOrder)
    {
        SysPay sysPay = new SysPay();
        Long gs = sysOrder.getOrderTime();
        Long rgf = gs*20 ;
        Long pjf = (long)0;
        Long zfy = null;
        int i =1;
        String pjs = "";
        sysPay.setOrderId(sysOrder.getOrderId());
        sysPay.setOrderTime(gs);
        sysPay.setArtificialCost(rgf);
        List<Long> goodsIds = sysOrder.getGoodsIds();
        System.out.println(goodsIds);
        for (Long goodsId : goodsIds) {
            String pj = sysGoodsMapper.selectSysGoodsByGoodsId(goodsId).getGoodsName();
            BigDecimal c = sysGoodsMapper.selectSysGoodsByGoodsId(goodsId).getGoodsPrice();
            pjs = pjs + i+ "、"+  pj +"("+c+"元)" ;
            pjf = pjf + c.longValue() ;
            i++;
        }
        sysPay.setGoodsCost(pjf);
        sysPay.setOrderGoods(pjs);
        zfy = pjf + rgf;
        sysPay.setPayTotal(zfy);
        sysPayMapper.insertSysPay(sysPay);
    }
    @Override
    public List<Long> selectGoodsListByOrderId(Long orderId) {
        return sysOrderGoodsMapper.selectOrderGoodsByOrderId(orderId);
    }
}
